﻿using INTEX.SCHEDULER;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    /// 
    public partial class VWVKP
    {
        double GDensity
        {
            get
            {
                return Convert.ToDouble(Density.Text) * 60;
            }
            set
            {
                Density.Text = value.ToString();
            }
        }
        double GDays = 60;
        Dictionary<string, object> MGANTTSLib
        {
            get
            {
                return Scheduler.MGANTTSLib;
            }
        }
        Dictionary<string, object> MachinesLib
        {
            get
            {
                return Scheduler.MachinesLib;
            }
        }
        Dictionary<string, object> JobsLib
        {
            get
            {
                return Scheduler.JobsLib;
            }
        }
        public void SetupMachine()
        {
            Scheduler.GDays = GDays;
            Scheduler.GDensity = GDensity;
            Scheduler.SetEnv = SetEnv;
            Scheduler.SetData = SetData;
            Scheduler.SetDetail = SetProd;
            Scheduler.Pagescope = pagescope;
            Scheduler.TaskScheduler = _sync;
            int count = ZoneCalendar.Children.Count;
            ZoneCalendar.Children.RemoveRange(0, count);
            Scheduler.DrawCalendar(ZoneCalendar);
            Scheduler.DrawMachine(MachineTable, machines); 
        }
        private void btnPlan_Click(object sender, RoutedEventArgs e)
        {
            AsyncRun("StartPlan", SetProd, SetEnv, SetData, MachinesLib, JobsLib);
        }
        void vwBMKRF_CurrentChanged(object sender, EventArgs e)
        {
            if (dwBMKRF == null)
                return;
            try
            {
                KRS = dwBMKRF["ABFAUFKRS"].ToString();
                LFD = dwBMKRF["ABFAUFLFD"].ToString();
                POS = dwBMKRF["ABFAUFPOS"].ToString();
                MKFTP = dwBMKRF["MKFTP"].ToString();
                string jobid = "Job" + dwBMKRF["ABAID"].ToString();// KRS + "_" + LFD + "_" + POS + "_" + dwJob["MKFTP"].ToString();
                JobBase job = (JobBase)this.FindName(jobid);
                if (job == null)
                {
                    return;
                }
                if (job.MGANTT == null)
                {
                    return;
                }
                string SMTYP = job.MGANTT.Machine.SMTYP;
                if (!FilterByMachineType) 
                {
                    SMTYP = "";
                }
                Scheduler.FilterByMachineType(SMTYP);
                btnSearch_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void RemoveAllJobs()
        {

            foreach (object obj in MGANTTSLib.Values)
            {
                MGANTT machine = (MGANTT)obj;
                if (machine != null)
                {
                    int count = machine.MainGrid.Children.Count;
                    if (count > 1)
                    {
                        machine.MainGrid.Children.RemoveRange(1, count - 1);
                    }
                }
            }
        }
        private void btnShowAllJob_Click(object sender, RoutedEventArgs e)
        {
            cvABP.CustomFilter = "JOBTYPE<>'X'";
        }
        private void btnJobFilter_Click(object sender, RoutedEventArgs e)
        {
            string filter = "JOBTYPE<>'X' AND ABFAUFKRS='" + KRS + "' AND ABFAUFLFD='" + LFD + "' AND ABFAUFPOS='" + POS + "'";
            cvABP.CustomFilter = filter;
        }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (Lastjob != null)
                {
                    UnFocusJob(Lastjob);
                }
                //将缸的所有分批找出来，并显示
                string filter = "ABFAUFKRS='" + KRS + "' AND ABFAUFLFD='" + LFD + "' AND ABFAUFPOS='" + POS + "'";

                DataRow[] rows = SetEnv.Tables["ICC_MKRF"].Select(filter);
                int count = rows.Length;
                foreach (DataRow dwJob in rows)
                {
                    string jobid = "Job" + dwJob["ABAID"].ToString();// KRS + "_" + LFD + "_" + POS + "_" + dwJob["MKFTP"].ToString();
                    JobBase job = (JobBase)this.FindName(jobid);
                    if (job == null)
                    {
                        return;
                    }
                    FocusJob(mainViewer, machines, job);
                    break;
                }
                //Scheduler.RefreshJobDB(KRS, LFD, POS, SetData, SetEnv, SetVKK, SetVKP, SetProd);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void FocusJob(AniScrollViewer viewer, StackPanel master, JobBase job)
        {

            Lastjob = job;
            job.Background = Brushes.Wheat;
            DropShadowEffect effect = new DropShadowEffect();
            effect.BlurRadius = 2;
            effect.Color = Color.FromRgb(255, 255, 0);
            job.Effect = effect;
            RotateTransform rt = new RotateTransform();
            rt.Angle = 0;
            TransformGroup tg = new TransformGroup();
            tg.Children.Add(rt);
            job.RenderTransform = tg;
            MGANTT MGANTT = (MGANTT)this.FindName(job.MGANTT.Name);
            Grid MainGrid = MGANTT.MainGrid;
            Point relativePoint = job.TransformToAncestor(MainGrid).Transform(new Point(0, 0));
            double x = relativePoint.X;
            relativePoint = MGANTT.TransformToAncestor(master).Transform(new Point(0, 0));
            double y = (relativePoint.Y);
            ScrollToPosition(viewer, x, y);
        }
        public void FocusMachine(AniScrollViewer viewer, StackPanel master, Machine job)
        {

            job.Background = Brushes.Wheat;
            DropShadowEffect effect = new DropShadowEffect();
            effect.BlurRadius = 10;
            effect.Color = Color.FromRgb(255, 255, 0);
            job.Effect = effect;
            RotateTransform rt = new RotateTransform();
            rt.Angle = -2;
            TransformGroup tg = new TransformGroup();
            tg.Children.Add(rt);
            job.RenderTransform = tg;

            Point relativePoint = job.TransformToAncestor(master).Transform(new Point(0, 0));
            ScrollToPosition(viewer, relativePoint.X, relativePoint.Y);
        }
        public void UnFocusJob(JobBase job)
        {
            job.Background = Brushes.White;
            job.Effect = null;
            job.RenderTransform = null;
        }
        public void ScrollToPosition(AniScrollViewer viewer, double x, double y)
        {
            //vwCalendar.ScrollToHorizontalOffset(x);
            //vwMachine.ScrollToVerticalOffset(y);
            DoubleAnimation vertAnim = new DoubleAnimation();
            vertAnim.From = viewer.VerticalOffset;
            vertAnim.To = y;
            vertAnim.DecelerationRatio = .2;
            vertAnim.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            DoubleAnimation horzAnim = new DoubleAnimation();
            horzAnim.From = viewer.HorizontalOffset;
            horzAnim.To = x;
            horzAnim.DecelerationRatio = .2;
            horzAnim.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            Storyboard sb = new Storyboard();
            sb.Children.Add(vertAnim);
            sb.Children.Add(horzAnim);
            Storyboard.SetTarget(vertAnim, viewer);
            //Storyboard.SetTarget(vwCalendar, viewer);
            Storyboard.SetTargetProperty(vertAnim, new PropertyPath(AniScrollViewer.CurrentVerticalOffsetProperty));
            Storyboard.SetTarget(horzAnim, viewer);
            //Storyboard.SetTarget(vwMachine, viewer);
            Storyboard.SetTargetProperty(horzAnim, new PropertyPath(AniScrollViewer.CurrentHorizontalOffsetProperty));
            sb.Begin();
        }
        private void Density_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem item = e.AddedItems[0] as ComboBoxItem;
            string txt = item.Content.ToString();
            txtDen.Text = txt + "小时";
            GDensity = Convert.ToInt16(txt);
            if (lastDen != txt)
            {
                Scheduler.ReSetDensity(GDensity);
            }
            lastDen = txt;
        }
        private void Current_Click(object sender, RoutedEventArgs e)
        {
            if (MGANTTSLib.Count == 0)
                return;

            foreach (KeyValuePair<string, object> m in MGANTTSLib)
            {
                MGANTT MGANTT = (MGANTT)m.Value;
                int current = Scheduler.MCALENDAR.CurrentBlock; 
                double x = current*Scheduler.MCALENDAR.PIXELS;
                ScrollToPosition(mainViewer, x, 0);
                break;
            }

        }
        private void mainViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            vwCalendar.ScrollToHorizontalOffset(mainViewer.HorizontalOffset);
            vwMachine.ScrollToVerticalOffset(mainViewer.VerticalOffset);
        }
        private void vwMachine_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            //mainViewer.ScrollToVerticalOffset(vwMachine.VerticalOffset);         
        }
    }
}
